//
//  ZYCLeaveRecordCollectionViewCell.m
//  ZDSC
//
//  Created by ZDTX on 2021/8/15.
//

#import "ZYCLeaveRecordCollectionViewCell.h"
#import "ZYCStuLeavePersonCell.h"
#import "ZYCStuLeaveDeptCell.h"
#import "ZYCRoleTreeModel.h"
@interface ZYCLeaveRecordCollectionViewCell ()<UITableViewDelegate,UITableViewDataSource>
{
    NSIndexPath *selectIndexPath;
}
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ZYCLeaveRecordCollectionViewCell

-(instancetype) initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
//        int R = (arc4random() % 256) ;
//        int G = (arc4random() % 256) ;
//        int B = (arc4random() % 256) ;
//        self.backgroundColor = [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1];
        [self.contentView addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }

    return self;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.model.children.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCRoleTreeModel *model = self.model.children[indexPath.row];
    if ([model.dateType integerValue] == 4) {
        ZYCStuLeavePersonCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCStuLeavePersonCell class]) forIndexPath:indexPath];
        cell.model = model;
        if (selectIndexPath == indexPath) {
            cell.isSelect = YES;
        }else{
            cell.isSelect = NO;
        }
        return cell;
    }else{
        ZYCStuLeaveDeptCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCStuLeaveDeptCell class]) forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZYCRoleTreeModel *model = self.model.children[indexPath.row];
    if ([model.dateType integerValue] == 4) {
        ZYCStuLeavePersonCell *cell = [tableView cellForRowAtIndexPath:selectIndexPath];
        cell.isSelect = NO;
        selectIndexPath = indexPath;
        NSLog(@"选中的人名字:%@",model.label);
        [self.tableView reloadData];
    }else{
        if ([self.delegate respondsToSelector:@selector(leaveRecordCollectionViewCellDidNext:)]) {
            [self.delegate leaveRecordCollectionViewCellDidNext:self];
        }
    }
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 48.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 48.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCStuLeavePersonCell class] forCellReuseIdentifier:NSStringFromClass([ZYCStuLeavePersonCell class])];
        [_tableView registerClass:[ZYCStuLeaveDeptCell class] forCellReuseIdentifier:NSStringFromClass([ZYCStuLeaveDeptCell class])];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
    }
    return _tableView;
}
@end
